package com.bs.resa.service.impl;

import com.bs.resa.dao.AdminUserMapper;
import com.bs.resa.dao.DepartmentMapper;
import com.bs.resa.pojo.Department;
import com.bs.resa.service.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lin
 * @date 2019/4/4 17:20
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    DepartmentMapper departmentMapper;
    @Resource
    AdminUserMapper adminUserMapper;
    /**
     * 查询部门员工
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAll() {
        List<Map<String,Object>> resultList=new ArrayList<>();
        Map<String,Object> map;
        List<Map<String,Object>> children;

        List<Department> departmentList=departmentMapper.queryAll();

        for(Department department:departmentList){
            map=new HashMap<>();
            children=new ArrayList<>();
            map.put("id",department.getId());
            map.put("deptname",department.getName());
            List<Map<String,Object>> userMap=departmentMapper.queryChild(department.getId());
            if(userMap!=null){
                for(Map<String,Object> user:userMap){
                    user.put("children",new ArrayList<>());
                    user.put("deptname",user.get("account"));
                    user.remove("account");
                    children.add(user);
                }
            }
            map.put("children",children);

            resultList.add(map);
        }
        return resultList;
    }
}
